<template>
	<div class="system-role-container layout-padding">
		<!-- <SearchForm :listQuery="state.listQuery" :formOption="state.formOption" @onSearch="onSearch" @resetSearch="resetSearch"></SearchForm> -->
		<div class="system-role-padding layout-padding-auto layout-padding-view">
			<div class="system-user-search mb15">
				<el-button size="default" type="primary" @click="onOpenAddCate('add')">
					<el-icon>
						<ele-Plus />
					</el-icon>
					新增
				</el-button>
			</div>
			<el-table
				:data="state.tableData"
				v-loading="state.loading"
				row-key="id"
				:tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
			>
				<el-table-column prop="name" label="分类名称">
				</el-table-column>
				<!-- <el-table-column prop="img" label="图片" align="center">
					<template #default="scope">
						<img :src="hotIcon" class="hot-icon" v-show="scope.row.is_hot === 2" />
						<el-image
							:src="scope.row.img"
							lazy
							style="width: 50px; height: 50px; border-radius: 50%"
							fit="cover"
							:preview-src-list="[scope.row.img]"
							:z-index="100"
							:preview-teleported="true"
						/>
					</template>
				</el-table-column> -->
				<el-table-column prop="status" label="分类状态" align="center">
					<template #default="scope">
						<el-tag type="success" v-if="scope.row.status === 'normal'">正常</el-tag>
						<el-tag type="info" v-else>下架</el-tag>
					</template>
				</el-table-column>
				<el-table-column prop="sort" label="排序" align="center"></el-table-column>
				<el-table-column prop="created_at" label="创建时间" align="center"></el-table-column>
				<el-table-column prop="updated_at" label="更新时间" align="center"></el-table-column>
				<el-table-column label="操作" width="240" fixed="right" header-align="center">
					<template #default="scope">
						<el-button size="default" type="primary" link @click="onOpenViewCate('view', scope.row.id)">查看</el-button>
						<el-button size="small" text type="primary" @click="onOpenAddNextCate('addNext', scope.row.id, scope.row.parentid)"
							>新增</el-button
						>
						<el-button  size="small" text type="primary" @click="onOpenEditCate('edit', scope.row.id)"
							>修改</el-button
						>
						<el-button
							v-show="scope.row.status === 'disable'"
							
							size="small"
							text
							type="primary"
							@click="operate('normal', scope.row, '确认上架当前分类吗？')"
							>上架</el-button
						>
						<el-button
							v-show="scope.row.status === 'normal'"
							
							size="small"
							text
							type="primary"
							@click="operate('disable', scope.row, '确认下架当前分类吗？')"
							>下架</el-button
						>
						<el-button  size="small" text type="primary" @click="onRowDel(scope.row)">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
		</div>
		<CateDialog ref="cateDialogRef" @refresh="getTableData()" />
	</div>
</template>

<script setup lang="ts" name="cateList">
import { defineAsyncComponent, reactive, onMounted, ref } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';
import { treeList, cateList, updateCate, deleteCate } from '/@/api/cate';
import hotIcon from '/@/assets/hot-icon.svg';

// 引入组件
const CateDialog = defineAsyncComponent(() => import('/@/views/news/cate/dialog.vue'));
// 搜索组件
const SearchForm = defineAsyncComponent(() => import('/@/components/searchForm/index.vue'));

// 定义变量内容
const cateDialogRef = ref();
const state = reactive({
	listQuery: {
		sort: 'sort',
		order: 'asc',
	},
	clearForm: {
		sort: 'sort',
		order: 'asc',
	},
	formOption: [
		{
			label: '名称',
			prop: 'name',
			element: 'input',
			option: {
				maxlength: 50,
			},
		},
		{
			label: '状态',
			prop: 'status',
			element: 'select',
			selectOption: [
				{ label: '正常', value: 'normal' },
				{ label: '已下架', value: 'disable' },
			],
		},
	],
	tableData: [],
	loading: false,
});

// 初始化信息
const initData = () => {};

// 初始化表格数据
const getTableData = () => {
	state.loading = true;
	treeList(state.listQuery).then((res: any) => {
		if (res.status === 'success') {
			state.tableData = res.data;
		}
	});
	setTimeout(() => {
		state.loading = false;
	}, 500);
};
// 打开查看弹窗
const onOpenViewCate = (type: string, id: number) => {
	cateDialogRef.value.openDialog(type, id, state.tableData);
};
// 打开新增弹窗
const onOpenAddCate = (type: string) => {
	cateDialogRef.value.openDialog(type, null, state.tableData);
};
// 打开新增下级弹窗
const onOpenAddNextCate = (type: string, id:number, parentid:string) => {
	cateDialogRef.value.openDialog(type, id, state.tableData, parentid);
};
// 打开修改弹窗
const onOpenEditCate = (type: string, id: number) => {
	cateDialogRef.value.openDialog(type, id, state.tableData);
};
// 删除
const onRowDel = (row: any) => {
	ElMessageBox.confirm(`此操作将永久删除分类名称：“${row.name}”，是否继续?`, '提示', {
		confirmButtonText: '确认',
		cancelButtonText: '取消',
		type: 'warning',
	})
		.then(() => {
			deleteCate(row.id).then((res: any) => {
				if (res.status === 'success') {
					ElMessage.success('删除成功');
					getTableData();
				} else {
					ElMessage.error(res.msg);
				}
			});
		})
		.catch(() => {});
};
// 分页数量改变
const onHandleSizeChange = (val: number) => {
	state.listQuery.size = val;
	getTableData();
};
// 当前页改变
const onHandleCurrentChange = (val: number) => {
	state.listQuery.page = val;
	getTableData();
};
// 搜索
const onSearch = () => {
	getTableData();
};

// 重置
const resetSearch = () => {
	state.listQuery = JSON.parse(JSON.stringify(state.clearForm));
	getTableData();
};
// 上架、下架、设为热门、取消热门
const operate = (type: string, row: any, msg: string) => {
	ElMessageBox.confirm(msg, '提示', {
		confirmButtonText: '确认',
		cancelButtonText: '取消',
	})
		.then(() => {
			let data = {};
			if (type === 'normal') {
				data = { id: row.id, status: 'normal' };
			} else if (type === 'disable') {
				data = { id: row.id, status: 'disable' };
			}
			updateCate(data).then((res: any) => {
				if (res.status === 'success') {
					ElMessage.success('操作成功');
					getTableData();
				} else {
					ElMessage.error(res.msg);
				}
			});
		})
		.catch(() => {});
};
// 页面加载时
onMounted(() => {
	initData();
	getTableData();
});
</script>

<style scoped lang="scss">
.system-role-container {
	.system-role-padding {
		padding: 15px;
		.el-table {
			flex: 1;
		}
	}

	.hot-icon {
		position: absolute;
		width: 24px;
		height: 24px;
		right: 25px;
		top: 2px;
	}
}
</style>
